Personal tools

Lua/Tutorials/GetAimTarget

From JC2-MP Documentation

< Lua‎ | Tutorials
Jump to: navigation, search


This function is available for the Player class, on both the server and client.

Each version returns a table containing the following:


entity can be one of the following types:

  • Player
  • Vehicle
  • StaticObject
  • ClientActor


To get the type of entity, you can use entity.__type, which returns a string, such as "Player".

Example

Server: Print player Bob's results of GetAimTarget every second

timer = Timer()
 
function Foo()
	if timer:GetSeconds() > 1 then
		timer:Restart()
 
		local results = Player.Match("Bob")[1]:GetAimTarget()
		print("position: "..tostring(results.position))
		print("entity: "..tostring(results.entity))
	end
end
 
Events:Subscribe("PostTick", Foo)

Client: Print what you're aiming at when you hold fire

function Foo(args)
	if args.input == Action.FireRight then
		local results = LocalPlayer:GetAimTarget()
		if results.entity then
			local entityType = results.entity.__type
			Chat:Print("You are aiming at a "..entityType..": "..tostring(results.entity), Color(255, 255, 255))
		end
	end
end
 
Events:Subscribe("LocalPlayerInput", Foo)

Update history

0.1.2

The player and vehicle table variables are deprecated in favor of a single entity variable. They both still work, however.

See also